# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'

import os
import logging

Logger = logging.getLogger('PySolFC.Games_MUGHAL')

from pysollib.utillib.misc import kwdefault
from pysollib.packlib.info import TYPE_MUGHAL_GANJIFA

__PULUGINS_DIR = os.path.dirname(os.path.realpath(__file__))
__PULUGINS_MOD = 'pysollib.games.mughal.%s'


def _parseinfo(game):
    """
    _parseinfo(game) -> dict

    Check the `__info__` special attribute of `game` and default ranks and
    suits for a Hex-aDeck deck in preparation for adding to the game database.

    :raises: AssertionError when `__info__` has category other than Hex-aDeck.
    """
    info = game.__info__
    info['gameclass'] = game
    assert info.get('category') is TYPE_MUGHAL_GANJIFA
    kwdefault(info, ranks=range(12), suits=range(8))
    return info


def loadgames():
    """
    loadgames() -> None

    Load games found in `PROVIDES` in each module not starting with `_`.
    """
    for filename in os.listdir(__PULUGINS_DIR):
        if filename.endswith('.py') and not filename.startswith('_'):
            module = __PULUGINS_MOD % filename[:-3]
            Logger.info('Loading: %s', module)
            try:
                gp = __import__(module, None, None, [''])
            except NameError as ex:
                Logger.warning('Error loading %s: %s', filename[:-3], ex)
                continue
            if not hasattr(gp, 'PROVIDES'):
                continue
            for game in gp.PROVIDES:
                yield(_parseinfo(game))